package com.guanlixitong.factory;

import com.guanlixitong.controller.IStuedntController;
import com.guanlixitong.controller.IUserController;
import com.guanlixitong.dao.IStudentDao;
import com.guanlixitong.dao.IUserDao;
import com.guanlixitong.service.IStudentService;
import com.guanlixitong.service.IUserService;

import java.io.IOException;
import java.util.Properties;

/*
工厂类
生产对象
 */
public class Factory {

    //prop集合加载配置文件
    public static Properties prop = new Properties();

    static {
        try {
            prop.load(ClassLoader.getSystemResourceAsStream("Bean.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //IUserController实现类对象
    public static IUserController getUserController(){

        IUserController controller = null;

        try {
            String userController = prop.getProperty("IUserController");
            Class clazz = Class.forName(userController);
            controller = (IUserController) clazz.getConstructor().newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return controller;
    }

    //IUserService实现类对象
    public static IUserService getUserService(){
        IUserService service = null;

        try {
            String userService = prop.getProperty("IUserService");
            Class clazz = Class.forName(userService);
            service = (IUserService) clazz.getConstructor().newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return service;
    }

    //IUserDao实现类对象
    public static IUserDao getUserDao(){
        IUserDao dao = null;

        try {
            String userDao = prop.getProperty("IUserDao");
            Class clazz = Class.forName(userDao);
            dao = (IUserDao) clazz.getConstructor().newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dao;
    }

    //IStuedntController实现类对象
    public static IStuedntController getStudentController()  {

        IStuedntController controller = null;

        try {
            String studentcontroller = prop.getProperty("IStudentController");
            Class clazz = Class.forName(studentcontroller);
             controller = (IStuedntController) clazz.getConstructor().newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return controller;
    }

    //IStudentService实现类对象
    public static IStudentService getStudentService(){
        IStudentService service = null;

        try {
            String studentcontroller = prop.getProperty("IStudentService");
            Class clazz = Class.forName(studentcontroller);
            service = (IStudentService) clazz.getConstructor().newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return service;
    }

    //IStudentDao实现类对象
    public static IStudentDao getStudentDao(){
        IStudentDao studentDao = null;

        try {
            String studentcontroller = prop.getProperty("IStudentDao");
            Class clazz = Class.forName(studentcontroller);
            studentDao = (IStudentDao) clazz.getConstructor().newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return studentDao;
    }

}

